package com.question.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


public class MyProperties {
	 
	 private String path = null;
	 
	 public MyProperties(){
		 path = "topic.properties";
	 } 
	 
	 public MyProperties(String path){
		 this.path = path;
	 }
	
	 /**
	  * 获取配置文件的信息	
	  * 
	 * @param str
	 * @return
	 * @throws IOException
	 */
	public String getProperties(String str) throws IOException {  
		 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);  
         Properties properties = new Properties();  
         try{  
             properties.load(inputStream);  
             return properties.getProperty(str);
         }catch (IOException ioE){  
             ioE.printStackTrace();  
         }finally{  
	         inputStream.close();  
         }  
         System.out.println("name:"+properties.getProperty("name"));
		return null;  
    }
	
	public void setProperties(String[] str){
		Properties prop = new Properties();
		String path = "src\\topic.properties";
		try {
			FileOutputStream oFile = new FileOutputStream(path, true);//true表示追加打开
			prop.setProperty("minSingle", ""+1);
			prop.setProperty("maxSingle", str[1]);
			prop.setProperty("minMultiple", str[2]);
			prop.setProperty("maxMultiple", str[3]);
			prop.setProperty("minTF", str[4]);
			prop.setProperty("maxTF", str[5]);
			prop.store(oFile, "The New properties file");
			oFile.close();
		} catch (IOException e) {
			System.err.println("Visit " + path + " for updating " + "ddd"
					+ " value error");
		}
	}
	 
	public Map<String, Integer> getMostMap(){
		Map<String, Integer> map = null;
		 try {
			 map = new HashMap<String, Integer>();
			 //用于计算总共有多少题
			int totalTopic = getIntProperties("totalTopic");
			map.put("totalTopic", totalTopic);

			//用于纪录一道单选题的分数
			 int singleGrade = getIntProperties("singleGrade");
			map.put("singleGrade", singleGrade);
			//用于纪录一道多选题的分数
			 int multipleGrade = getIntProperties("multipleGrade");
			map.put("multipleGrade", multipleGrade);
			//用于纪录一道判断题的分数
			 int TFGrade = getIntProperties("TFGrade");
			map.put("TFGrade", TFGrade);
			//用于纪录单选题的数目
			 int singleTopic = getIntProperties("singleTopic");
			map.put("singleTopic", singleTopic);
			//用于纪录多选题的数目
			 int multipleTopic = getIntProperties("multipleTopic");
			map.put("multipleTopic", multipleTopic);
			//用于纪录判断题的数目
			 int TFTopic = getIntProperties("TFTopic");
			 map.put("TFTopic", TFTopic);
			//用于纪录最小的单选题
			 int minSingle = getIntProperties("minSingle");
			 map.put("minSingle", minSingle);
			//用于纪录最大的单选题
			 int maxSingle = getIntProperties("maxSingle");
			 map.put("maxSingle", maxSingle);
			//用于纪录最小的多选题
			 int minMultiple = getIntProperties("minMultiple");
			 map.put("minMultiple", minMultiple);
			//用于记录最大的多选题
			 int maxMultiple = getIntProperties("maxMultiple");
			 map.put("maxMultiple", maxMultiple);
			//用于记录最小的判断题
			 int minTF = getIntProperties("minTF");
			 map.put("minTF", minTF);
			//用于记录最大的判断题
			 int maxTF = getIntProperties("maxTF");
			 map.put("maxTF", maxTF);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
		
	}
	
	 /**
	  * 将从配置文件获取的数据转化为int型数据
	  * 
	 * @param str
	 * @return
	 * @throws IOException
	 */
	public int getIntProperties(String str) throws  IOException{
		 return Integer.valueOf(getProperties(str));
	 }
	 
    public static void main(String[] args) throws IOException{  
    	Properties prop = new Properties();
    	MyProperties pt = new MyProperties();  
        String[] a = new String[1];
        a[0] = new String("fkal");
		pt.setProperties(a);
        //System.out.println(pt.getProperties("minSingle"));    
    }  
    
}
